
from flask import Flask,request
from ToDos.todo_app_with_mysql.storage_mysql import TodoStorageWithMySQL
app=Flask(__name__)
app.config['JSON_AS_ASCII']=False

todo=TodoStorageWithMySQL()

@app.route('/')
def hello_world():
    return "<P>欢迎使用记事本小程序</p>"

@app.route('/todos',methods=['GET'])
def get_todo_list():
    print(request.args.get("completed"))
    if request.args.get("completed")==None:
        return {'todos': [x.__dict__ for x in todo.unfinished_todo()]}
    elif request.args.get("completed")=='1':
        return {'todos': [x.__dict__ for x in todo.comepleted_todo()]}
    else:
        return {'todos': [x.__dict__ for x in todo.list_todos()]}



# 新建事项
@app.route('/create', methods=['POST'])
def create_todo():
    if 'title' not in request.form:
        return '参数错误，必须提供 title 参数', 400

    do=todo.create(request.form['title'])
    return do.__dict__


@app.route("/completed", methods=["GET"])
def mark_completed():
    if "id" not in request.args:
        return "参数错误，必须提供 id参数", 400
    else:
        id=int(request.args["id"])
        mark=todo.mark_completed(id)
        return mark.__dict__


@app.route("/delete", methods=["GET"])
def delete():
    if "id" not in request.args:
        return "参数错误，必须提供 id参数", 400
    else:
        id=int(request.args["id"])
        id = int(request.args["id"])
        todo.delete(id)
        return "成功删除"




if __name__ == '__main__':
    app.run()